import { createSlice } from "@reduxjs/toolkit"
import type { ICustomerItem } from "../interface/IlistItem";
// 片区二
const CustomerSlice = createSlice({
    // 片区名称
    name: "customer",
    // 片区初始状态
    initialState: {
        customer: localStorage.customer ? JSON.parse(localStorage.customer) : [],
    },
    // 片区操作
    reducers: {
        setcustomerInfo(state, action) {
            state.customer.push(action.payload)
            localStorage.customer = JSON.stringify(state.customer)
        },
        editcustomerInfo(state, action) {
            const index = state.customer.findIndex((item:ICustomerItem) => item.id === action.payload.id)
            state.customer.splice(index, 1, action.payload)
            localStorage.customer = JSON.stringify(state.customer)
        },
        delcustomerInfo(state, action) {
            const index = state.customer.findIndex((item:ICustomerItem) => item.id == action.payload.id)
            state.customer.splice(index, 1)
            localStorage.customer = JSON.stringify(state.customer)
        }
    }
});
export const { setcustomerInfo, editcustomerInfo, delcustomerInfo } = CustomerSlice.actions;
export default CustomerSlice;